<template>
  <div class="pad_10">
    <el-form ref="queryRef" :inline="true" label-width="68px">
      <el-form-item>
        <!-- <el-button @click="handleQuery">保存</el-button> -->
        <el-button @click="Cancel">取消</el-button>
        <el-button @click="cliCAuditFun">同意</el-button>
        <el-button @click="cliRefuseFun">驳回</el-button>
        
        <!-- <el-button :disabled="!route.query.id" @click="auditFun">审核</el-button> -->
      </el-form-item>
    </el-form>
    <el-divider />
    <SetUserSelect
      :key="keys"
      ref="setUserSelect"
      class="queryForm"
      :labelwidth="100"
      :isShowUserBtn="false"
      :buttonIsShow="false"
      :formData="bill"
      :queryParams="datas"
      @selChangeFun="changeFun"
    ></SetUserSelect>
    <cliRefuseDom ref="cliRefuseRef" :id="route.query.id" url="/task/apply/refuse" @updateData="updateData"></cliRefuseDom>
  </div>
</template>
<script setup lang="ts">
import { ref, getCurrentInstance, watch } from "vue";
import { getprovince } from "@/api/basicManagement/hospital";
import { drugTypeList  } from "@/api/basicManagement/categoryMana";
import { drugList  } from "@/api/basicManagement/varietyMana";
import { addapply, applyId, eitHapply, cliCAudit, cliRefuse  } from "@/api/missionPlan";
import { agencyList  } from "@/api/basicManagement/agencyMana";
import { hospitalList  } from "@/api/basicManagement/hospital";
import data from "./data";
import { useRoute } from "vue-router";
const { bill, datas } = data;
datas.value = {
  hospitalList: [],
  status: '1'
}
const { proxy } = getCurrentInstance();
let proxys: any = proxy;
const route: any = useRoute();
let keys = ref<any>("");
const { timed_task_type, mission_plan_status } = proxys.useDict(
  "timed_task_type",
  "mission_plan_status"
);
watch(timed_task_type, (newV, oldV) => {
  bill.value[1].selectData = timed_task_type;
  keys.value = new Date().getTime();
});
// watch(timed_task_category, (newV, oldV) => {
//   bill.value[2].selectData = timed_task_category;
//   keys.value = new Date().getTime();
// });
watch(mission_plan_status, (newV, oldV) => {
  bill.value[6].selectData = mission_plan_status;
  keys.value = new Date().getTime();
});
const getprovinceFun = () => {
  getprovince().then((res:any) => {
    res.data?.forEach((val) => {
      val.value = val.cityname
      val.label = val.cityname
    })
    bill.value[3].selectData = res.data
    keys.value = new Date().getTime();
  })
}
getprovinceFun()

const doctorListFun = () => {
  hospitalList({
    pageNum: 1,
    pageSize: 10000,
  }).then((res:any) => {
    res.rows?.forEach((val) => {
      val.hospitalName = val.name
      val.hospitalId = val.id
    })
    bill.value[12].selectData = res.rows
    keys.value = new Date().getTime();
  })
}
doctorListFun()
let setHospitalList = ref([])
const applyIdFun = () => {
    applyId(route.query.id).then((res) => {
    res.data.status = "" + res.data.status;
    res.data.type = "" + res.data.type;
    res.data.setTime = res.data.startTime
    res.data.setTimemin = res.data.endTime
    setHospitalList.value = JSON.parse(JSON.stringify(res.data.hospitalList))
    res.data.hospitalList = res.data.hospitalList.map(item => item.hospitalId)
    datas.value = res.data;
  });
  }
route.query.id && (applyIdFun())
  

  
const handleQuery = function (): void {
  proxys.$refs.setUserSelect.submitForm().then((valid) => {
    const hospitalList:any = [];
    let hospital:any = '';
    valid.hospitalList.forEach((val, index) => {
      hospitalList.push({
        hospitalId: val,
        hospitalName: (bill.value[11].selectData.filter(item => item.id == val))[0].name,
        id: (setHospitalList.value.filter(item => item.hospitalId == val))[0]?.id ?? undefined,
        parentId: (setHospitalList.value.filter(item => item.hospitalId == val))[0]?.parentId ?? undefined,
      })
      if (index == valid.hospitalList.length-1) {
        hospital+=(bill.value[11].selectData.filter(item => item.id == val))[0].name;
      } else {
        hospital+=(bill.value[11].selectData.filter(item => item.id == val))[0].name + ',';
      }
    });
    (!route.query.id ? addapply : eitHapply)({
      ...valid,
      hospitalList,
      hospital
    }).then(() => {
      proxys.$modal.msgSuccess(`${route.query.id ? "修改" : "新增"}成功`);
      Cancel();
    });
  });
};
const changeFun = function (e: any): void {
  console.log(e);
};
const Cancel = () => {
  datas.value = {};
  const obj = { path: "/missionPlan/missionPlanList" };
  proxys.$tab.closeOpenPage(obj);
};
const drugTypeListFun = () => {
  drugTypeList({
    pageNum: 1,
    pageSize: 100000,
  }).then((res:any) => {
    res.rows.length && res.rows.forEach((item) => {
      item.value = item.id
      item.label = item.name
    })
    bill.value[2].selectData = res.rows
    keys.value = new Date().getTime();
  })
}
drugTypeListFun()

const cliCAuditFun = () => {
  if (datas.value.status =='0') {
    cliCAudit({
      id: route.query.id
    }).then(() => {
      proxys.$modal.msgSuccess('已同意')
      applyIdFun()
    })
  } else {
    proxys.$modal.msgWarning('已申请的任务才能同意')
  }
  
}
const cliRefuseFun = () => {
  if (datas.value.status =='0') {
    let fun: any = proxy.$refs.cliRefuseRef;
    fun.show();
  } else {
    proxys.$modal.msgWarning('已申请的任务才能驳回')
  }
  
};
const updateData = () => {
  applyIdFun()
}
</script>
<style scoped>
.pad_10 {
  padding: 10px;
}
.pad_10 .el-form--inline .el-form-item {
  margin: 0;
}
.el-divider--horizontal {
  margin: 10px 0;
}
</style>